import { UI } from '@hyext/hy-ui'
import React, { Component } from 'react'
import './app.hycss'
import Wait from './component/wait'
import {Jump} from './component/show_s'
import Show from './component/show_s'
import {sendYz, setResult} from '../utils/request'
// import {getUserResult} from '../utils/request'
import {getOrders, changeToIndex} from '../utils/request'
import IMG from '../img/img'
import Yz_item_view from './component/yz_item_view'
const hyExt = global.hyExt;

const { View, Text, Button, Input, ScrollView, Image, ImageBackground} = UI

class App extends Component {
  constructor () {
    super()

    this.state = {
      counted_result: '',
      msg: '2',
      orders_str: '',
      zt: '0',
      time: 0,
      // items_name: ['螃蟹', '鱼儿', '虾米', '铜钱', '公鸡', '葫芦'], 
      items_name: ['PX', 'YE', 'XM', 'TQ', 'GJ', 'HL'], 
      textBar: '',
      req_msg: '',
      yz_list:[], 
      time_send: 10,
      show_view: [],
      show_list: [],
      show_result_view: [],
      yz_list_view: []
    }
  }



  componentDidMount() {
    this.state.yz_list = [this.state.items_name[0], this.state.items_name[1]]
    let abc = this.state.yz_list.map((item, index)=>{
      return (<Yz_item_view index = {index} img = {IMG}  item = {item}></Yz_item_view>)
    })
    this.setState({yz_list_view: abc})
  }



  addYzItem(item_name){
    let len = this.state.yz_list.length
    if(len<6){
      this.state.yz_list.push(this.state.items_name[len])
    }else{
      this.state.yz_list = [this.state.items_name[0], this.state.items_name[1]]
    }
    let abc = this.state.yz_list.map((item, index)=>{
      return (<Yz_item_view index = {index} img = {IMG}  item = {item}></Yz_item_view>)
    })
    this.setState({yz_list_view: abc})
  }//addYzItem end

  my_start(){
    console.log(this.state.yz_list+"long"+this.state.yz_list.length)
    setResult(this.state.yz_list.length)
    let a = {data:this.state.yz_list, time: this.state.time_send}
    hyExt.observer.emit('yz',JSON.stringify(a)).then((res)=>{
      console.log("开始押注！！！"+"哈哈哈")
      console.log("开始押注！！！"+"哈哈哈"+JSON.stringify(a))
      //把押注项数据存储到内存中，以便观众端加载小程序时获取
      hyExt.storage.setItem('list', JSON.stringify(a)).then(() => {  
      })
    }
    ).catch((err)=>{
      console.log(err);
    });

    //把start_str存储到内存中，以便观众端加载小程序时获取
    hyExt.storage.setItem('start_str', '1').then(() => {  
    }).catch(err => {
    })

    //将时间存储到内存中
    hyExt.storage.setItem('time', this.state.time_send).then(() => {  
    }).catch(err => {
    })

    //跳转到wait页面（设置状态为wait）
    this.setState({zt: '1'})
  }//mystart end

/**我的render */
  renderMe(){
    return (
      <View className = "myview">
          <Button className = "btn2" onPress = {()=>{this.addYzItem("asdasd")}}>添加猜测项</Button>
          <Button className = "btn2" onPress = {()=>{this.my_start()}}>开始</Button>
          <View>
            <Text>{this.state.req_msg}</Text>
            <ScrollView>
              {this.state.yz_list_view}
            </ScrollView>
          </View>
      </View>
    )
  }
  /**我的render结束 */
  
  stop(){
    hyExt.storage.setItem("time", 0).then(()=>{}).catch(err=>{
      console.log("##############设置time到虎牙存储失败了#######################"+err)
    })
    this.setState({time: 0})
    this.setState({zt: '2'})
    //把start_str存储到内存中，以便观众端加载小程序时获取
    hyExt.storage.setItem('start_str', '0').then(() => {  
    }).catch(err => {
    })
    //押注时间到，即将开奖
    getOrders(10, this)
    // console.log("开奖结果排行："+this.state.orders_str)
    this.setState({zt: '2'})
    //从虎牙存储中获取已经计算过的结果并设置到state中
    hyExt.storage.getItem('counted_result').then(result=>{
      result = JSON.parse(result)
      let abc = result.result.map((item, index)=>{
        let indexs = changeToIndex(item)
        console.log(item+"indexs")
        return (<Yz_item_view index = {indexs} img = {IMG}  item = {item}></Yz_item_view>)
      })
      this.setState({counted_result: result})
      this.setState({show_result_view: abc})
    })
    //设置虎牙存储的time为0
    hyExt.storage.setItem('time', t).then(() => {  
    }).catch(err => {
      console.log("*****************************"+"失败主播端set******************************")
    })
  }//stop end


  render () {
    if(this.state.zt == '0'){
      return this.renderMe()
    }else if(this.state.zt == '1'){
      return (
        <Wait state={this.state} stop={()=>{this.stop()}}/>
      )
    }else {
      return (
        <Show jump_m = {()=>Jump(this)} show_view = {this.state.show_view} app = {this}/>
      )
    }
    
  }
}

export default App;